package system

import (
	"gin-admin/common/dto/response"
	"gin-admin/global"
	"gin-admin/service/dto/request"
	"gin-admin/service/v1/system"

	"github.com/gin-gonic/gin"
)

type RoleHandler struct {
	RoleService system.RoleService
}

var RoleHandlerApp = &RoleHandler{
	RoleService: system.RoleService{},
}

// @Summary 创建角色
// @Tags /v1/角色管理
// @Accept json
// @Product json
// @Param data body request.CreateRoleRequest true "角色信息"
// @Router /api/v1/role/create [post]
func (rh *RoleHandler) CreateRoleHandler(ctx *gin.Context) {
	var createRoleRequest request.CreateRoleRequest
	if err := ctx.ShouldBindJSON(&createRoleRequest); err != nil {
		global.GA_LOGGER.Sugar().Errorw("参数校验失败", "error", err)
		response.Failed(ctx)
		return
	}
	err := rh.RoleService.CreateRoleService(createRoleRequest)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.SuccessNoData(ctx)
}

// @Summary 删除角色信息
// @Tags /v1/角色管理
// @Accept json
// @Product json
// @Param id query string true "角色id"
// @Router /api/v1/role/delete [delete]
func (rh *RoleHandler) DeleteRoleHandler(ctx *gin.Context) {
	var deleteRoleRequest request.RoleDetailRequest
	if err := ctx.ShouldBindQuery(&deleteRoleRequest); err != nil {
		global.GA_LOGGER.Sugar().Errorw("参数校验失败", "error", err)
		response.Failed(ctx)
		return
	}
	err := rh.RoleService.DeleteRoleService(deleteRoleRequest)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.SuccessNoData(ctx)
}

// @Summary 更新角色信息
// @Tags /v1/角色管理
// @Accept json
// @Product json
// @Param data body request.ModifyRoleRequest true "角色信息"
// @Router /api/v1/role/modify [put]
func (rh *RoleHandler) ModifyRoleHandler(ctx *gin.Context) {
	var updateRoleRequest request.ModifyRoleRequest
	if err := ctx.ShouldBindJSON(&updateRoleRequest); err != nil {
		global.GA_LOGGER.Sugar().Errorw("参数校验失败", "error", err)
		response.Failed(ctx)
		return
	}
	err := rh.RoleService.UpdateRoleService(updateRoleRequest)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.SuccessNoData(ctx)
}

// @Summary 获取角色信息
// @Tags /v1/角色管理
// @Accept json
// @Product json
// @Param id query request.RoleDetailRequest true "角色信息请求参数"
// @Router /api/v1/role/info [get]
func (rh *RoleHandler) GetRoleInfoHandler(ctx *gin.Context) {
	var roleDetailRequest request.RoleDetailRequest
	if err := ctx.ShouldBindQuery(&roleDetailRequest); err != nil {
		global.GA_LOGGER.Sugar().Errorw("参数校验失败", "error", err)
		response.Failed(ctx)
		return
	}
	result, err := rh.RoleService.GetRoleInfoService(roleDetailRequest)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.Success(ctx, result)
}

// @Summary 获取角色列表
// @Tags /v1/角色管理
// @Accept json
// @Product json
// @Param data body request.RoleListRequest true "角色列表请求参数"
// @Router /api/v1/role/list [get]
func (rh *RoleHandler) GetRoleListHandler(ctx *gin.Context) {
	var roleListRequest request.RoleListRequest
	if err := ctx.ShouldBindJSON(&roleListRequest); err != nil {
		global.GA_LOGGER.Sugar().Errorw("参数校验失败", "error", err)
		response.Failed(ctx)
		return
	}
	result, code, err := rh.RoleService.GetRoleListService(roleListRequest)
	if err != nil {
		response.FailedWithCode(ctx, code)
		return
	}
	response.SuccessWithList(ctx, result.Page, result.Size, result.Total, result.List)
}
